/* *************************************** */
/* * Set Utilities Source                * */
/* * Authors:                            * */
/* *  André Bannwart Perina      7152757 * */
/* *  Rodrigo de Freitas Pereira 7573472 * */
/* *  Thaís Emanuele dos Santos  6453087 * */
/* *************************************** */

#include "../include/set.h"

#include <omp.h>
#include <stdlib.h>

/* Create a Set */
void set_create(set_t **set, unsigned int size) {
	*set = malloc(sizeof(set_t));

	(*set)->size = size;
	(*set)->set = calloc(size, sizeof(set_t));
}

/* Insert i on a Set. If the element already exists, nothing happens */
int set_insert(set_t **set, unsigned int i) {
	if(i >= (*set)->size)
		return SET_OUT_OF_BOUNDS;

	(*set)->set[i] = 1;

	return SET_SUCCESS;
}

/* Return true if Set contains element i */
int set_contains(set_t *set, unsigned int i) {
	if(i >= set->size)
		return SET_OUT_OF_BOUNDS;
	else if(set->set[i])
		return SET_CONTAINS;
	else
		return SET_DOESNT_CONTAINS;
}

/* Remove i from the Set, if any */
int set_remove(set_t **set, unsigned int i) {
	if(i >= (*set)->size)
		return SET_OUT_OF_BOUNDS;

	(*set)->set[i] = 0;

	return SET_SUCCESS;
}

/* Return the cardinality of the Set */
int set_cardinality(set_t *set) {
	int i, card;

	for(i = 0, card = 0; i < set->size; i++)
		card += set->set[i];

	return card;
}

/* Destroy Set */
void set_destroy(set_t **set) {
	free((*set)->set);
	free(*set);
}